<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ImportButton;
use App\Admin\Repositories\Journal;
use App\Jobs\JournalImport;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\EasyExcel\Excel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class JournalController extends AdminController
{

    public function handleImport(Request $request)
    {
        if (empty($request->file('file'))) {
            admin_error('请选择文件');
        }
        $file = $request->file('file');
        $entension =  $file->getClientOriginalExtension();

        $limit = ['xlsx', 'xls'];
        if (!in_array($entension, $limit)) {
            admin_error('文件类型有误, 请上传excel类型文件');
        }

        $tempPath = $file->store('temp');
        //   dd(storage_path('app') . DIRECTORY_SEPARATOR . 'new-' . $tempPath);
        // dd($tempPath);
        // $rows = Excel::import($file)->first()->toArray();
        // // 仍队列去写入表
        // JournalImport::dispatch($rows)->onQueue('journal-import');
        JournalImport::dispatch($tempPath)->onQueue('journal-import');

        return success_message('正在导入数据库······');
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Journal(), function (Grid $grid) {
            $grid->scrollbarX();

            $grid->model()->orderBy('id', 'desc');
            $grid->tools(new ImportButton('导入期刊', '/journal/handleImport'));

            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('issn_eissn');
            $grid->column('type');
            $grid->column('jcr_abbreviation');
            $grid->column('total_citations');
            $grid->column('jif_2022');
            $grid->column('jif_quartile');
            $grid->column('jci_2022');
            $grid->column('oa_gold');
            $grid->column('sjr');
            $grid->column('h_index');
            $grid->column('cite_score');
            $grid->column('snip');
            $grid->column('public_cycle');
            $grid->column('type_label');
            $grid->column('open_access');
            $grid->column('public_difficult');
            $grid->column('publisher');
            $grid->column('country_area');
            $grid->column('acceptance_time');
            $grid->column('website');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            // 通过期刊名、ISSN、影响因子、大类学科、是否OA期刊进行查询
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('name');
                $filter->like('issn_eissn');
                $filter->like('jif_2022');
                $filter->like('type');
                $filter->like('open_access', '是否OA')->select(['yes' => 'yes', 'no' => 'no']);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Journal(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('issn_eissn');
            $show->field('type');
            $show->field('jcr_abbreviation');
            $show->field('total_citations');
            $show->field('jif_2022');
            $show->field('jif_quartile');
            $show->field('jci_2022');
            $show->field('oa_gold');
            $show->field('sjr');
            $show->field('h_index');
            $show->field('cite_score');
            $show->field('snip');
            $show->field('public_cycle');
            $show->field('type_label');
            $show->field('open_access');
            $show->field('public_difficult');
            $show->field('publisher');
            $show->field('country_area');
            $show->field('acceptance_time');
            $show->field('website');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Journal(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('issn_eissn');
            $form->text('type');
            $form->text('jcr_abbreviation');
            $form->text('total_citations');
            $form->text('jif_2022');
            $form->text('jif_quartile');
            $form->text('jci_2022');
            $form->text('oa_gold');
            $form->text('sjr');
            $form->text('h_index');
            $form->text('cite_score');
            $form->text('snip');
            $form->text('public_cycle');
            $form->text('type_label');
            $form->text('open_access');
            $form->text('public_difficult');
            $form->text('publisher');
            $form->text('country_area');
            $form->datetime('acceptance_time');
            $form->url('website');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
